#include <head.h>
#include "mk_pthread.h"
#include <pthread.h>
#include "pthread_info.h"
#include "create_pthread.h"
#define LOG_TAG
#define log_xxx(format, arg...)	do { printf(LOG_TAG "[DEBUG]%d,%s--- " format "\n",\
										__LINE__,__func__, ## arg);} \
									while (0)

static void print_thread_info( pthread_t t,const char* tag)
{
    pthread_attr_t attr;
    int policy=0;
    struct sched_param param;
    // display_thread_attributes(t,(char*)tag);
    pthread_getattr_np(pthread_self(), &attr);
	printf("\n%s Thread attributes:\n",tag);
    display_pthread_attr(&attr,(char*)tag);
    pthread_getschedparam(t,&policy,&param);
    printf("[%s]policy=%d,sched_priority=%d\n",tag,policy,param.sched_priority);
    if(policy == SCHED_OTHER)
	{
		printf("[%s]shed policy:SCHED_OTHER\r\n",tag);
	}
	else if(policy == SCHED_FIFO )
	{
		printf("[%s]shed policy:SCHED_FIFO\r\n",tag);
	}
	else if(policy == SCHED_RR  )
	{
		printf("[%s]shed policy:SCHED_RR\r\n",tag);
	}
	else
	{
		printf("[%s] shed unknow\r\n",tag);
	}
    #if 0
————————————————
版权声明：本文为CSDN博主「知无止境」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/weixin_39270987/article/details/107646275
#endif
    pthread_attr_destroy(&attr);

}
static void *thread1(void *arg)
{
    const char *tag =(const char *)arg;
    print_thread_info(pthread_self(),tag);
    return (NULL);
}


static int mk_thread_test(int argc,char*argv[])
{
    int ret =0;
    pthread_t t1;
    ret =mk_pthread_default(&t1,thread1,"thread1");
    if(ret<0){
        log_xxx("create thread failed!");
    }

    pthread_t t2;
    ret =mk_pthread_default(&t2,thread1,"thread2");
    if(ret<0){
        log_xxx("create thread failed!");
    }

    pthread_t t3;
    ret =mk_pthread_priority(&t3,thread1,"thread3",20);
    if(ret<0){
        log_xxx("create thread failed!");
    }

    pthread_t t4;
    //failed on ubuntu16 server 4.4.0-186-generic
    ret =mk_pthread_realtime_default(&t4,thread1,"thread4");
    if(ret<0){
        log_xxx("create thread failed!");
    }

    pthread_t t5;
    ret =mk_pthread_priority_stack(&t5,thread1,"thread5",1024*1024*4,50);
    if(ret<0){
        log_xxx("create thread failed!");
    }

   
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    pthread_join(t3,NULL);
    pthread_join(t4,NULL);
    pthread_join(t5,NULL);

    return (0);
}

 
 
static void *start_routine(void *arg)
{
	int i, j;
    print_thread_info(pthread_self(),(char*)arg);

	while(1)
	{
		fprintf(stderr, "%c ", *(char *)arg);
		for(i=0; i<100000; i++)
			for(j=0; j<1000; j++);
	}
 
	pthread_exit(NULL);
}
 
 
static int simplt_test1(void)
{
 
	pthread_t tid1, tid2, tid3;
	pthread_attr_t attr1, attr2;
	struct sched_param param1, param2;
 
	/* 线程属性变量的初始化 */
	pthread_attr_init(&attr1);
	pthread_attr_init(&attr2);
 
 
	/* 设置线程是否继承创建者的调度策略 PTHREAD_EXPLICIT_SCHED：不继承才能设置线程的调度策略*/
	errno = pthread_attr_setinheritsched(&attr1, PTHREAD_EXPLICIT_SCHED);
	if(errno != 0)
	{
		perror("setinherit failed\n");
		return -1;
	}
 
	/* 设置线程是否继承创建者的调度策略 PTHREAD_EXPLICIT_SCHED：不继承才能设置线程的调度策略*/
	errno = pthread_attr_setinheritsched(&attr2, PTHREAD_EXPLICIT_SCHED);
	if(errno != 0)
	{
		perror("setinherit failed\n");
		return -1;
	}
 
	/* 设置线程的调度策略：SCHED_FIFO：抢占性调度; SCHED_RR：轮寻式调度；SCHED_OTHER：非实时线程调度策略*/
	errno = pthread_attr_setschedpolicy(&attr1, SCHED_RR);
	if(errno != 0)
	{
		perror("setpolicy failed\n");
		return -1;
	}
 
	errno = pthread_attr_setschedpolicy(&attr2, SCHED_RR);
	if(errno != 0)
	{
		perror("setpolicy failed\n");
		return -1;
	}
 
	//设置优先级的级别
	param1.sched_priority = 1;
	param2.sched_priority = 1;
	
	//查看抢占性调度策略的最小跟最大静态优先级的值是多少
	printf("SCHED_FIFO min=%d, max=%d\n", sched_get_priority_min(SCHED_FIFO), sched_get_priority_max(SCHED_FIFO));
 	printf("SCHED_RR min=%d, max=%d\n", sched_get_priority_min(SCHED_RR), sched_get_priority_max(SCHED_RR));

 
	/* 设置线程静态优先级 */
	errno = pthread_attr_setschedparam(&attr1, &param1);
	if(errno != 0)
	{
		perror("setparam failed\n");
		return -1;
	}
 
	errno = pthread_attr_setschedparam(&attr2, &param2);
	if(errno != 0)
	{
		perror("setparam failed\n");
		return -1;
	}
 
/* 创建三个测试线程 */
    /* 线程1，优先级1 */
	errno = pthread_create(&tid1, &attr1, start_routine, (void *)"1");
	if(errno != 0)
	{
		perror("create thread 1 failed\n");
		return -1;
	}
    /* 线程2，优先级1 */
	errno = pthread_create(&tid2, &attr2, start_routine, (void *)"2");
	if(errno != 0)
	{
		perror("create thread 2 failed\n");
		return -1;
	}
    /* 线程3，非实时线程，静态优先级0 */
	errno = pthread_create(&tid3, NULL, start_routine, (void *)"3");
	if(errno != 0)
	{
		perror("create thread 3 failed\n");
		return -1;
	}
 
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	pthread_join(tid3, NULL);
 
	pthread_attr_destroy(&attr1);
	pthread_attr_destroy(&attr2);
 
	return 0;
}
int main()
{
    // mk_thread_test(0,NULL);
    create_pthread_fun_test();
    return 0;
}